﻿using System.Collections.Generic;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class Cache<TInput, TResult>
    {
        private readonly Func<TInput, TResult> _factory;

        public Cache(Func<TInput, TResult> factory)
        {
            _factory = factory;
        }
        Dictionary<TInput, TResult> _cache = new Dictionary<TInput, TResult>();
        public TResult this[TInput item]
        {
            get
            {
                TResult result;
                if (!_cache.TryGetValue(item, out result))
                {
                    result = _factory(item);
                    _cache.Add(item, result);
                }
                return result;
            }
        }
    }
}